package com.yjc.drygoods.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.product.entity.CategoryEntity;
import com.yjc.drygoods.product.service.CategoryService;
import com.yjc.common.utils.R;


/**
 * 商品三级分类
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 21:27:48
 */
@Slf4j
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 查询出指定spuIdd的完整三级分类路径加上自身的
     */
    @GetMapping("/get/total/catelogPathWithSpuId/{spuId}")
    public R getTotalCatelogPathWithSpuId(@PathVariable("spuId") Long spuId) {
        List<Long> catelogPathWithSpuId = categoryService.findCatelogPathWithSpuId(spuId);
        log.info("catelogPathWithSpuId:{}", catelogPathWithSpuId);
        return R.ok().setData(catelogPathWithSpuId);
    }

    /**
     * 查出所有分类以及其子分类，以树形列表显示出来
     */
    @RequestMapping("/list/tree")
    public R list(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> listTree = categoryService.listWithTree();
        return R.ok().put("data", listTree);
    }

    /**
     * 查出所有分类以及其子分类以及三级分类下所有的spu干货，以树形列表显示出来（APP）
     */
    @GetMapping("/app/with/spu/list/tree")
    public R appListWithSpu(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> listTree = categoryService.appListWithSpuTree();
        log.info("listTreeApp:{}", listTree);
        return R.ok().put("data", listTree);
    }

    /**
     * 查出所有分类以及其子分类以及三级分类下所有的spu干货，以树形列表显示出来
     */
    @RequestMapping("/with/spu/list/tree")
    public R listWithSpu(@RequestParam Map<String, Object> params) {
        List<CategoryEntity> listTree = categoryService.listWithSpuTree();
        return R.ok().put("data", listTree);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 批量修改层级和排序
     */
    @RequestMapping("/update/sort")
    public R sortUpdate(@RequestBody List<CategoryEntity> categorys) {
        categoryService.updateBatchById(categorys);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
//        categoryService.removeByIds(Arrays.asList(catIds));

        //1.检查当前删除的菜单是否被其他地方引用
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
